package Items;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class ExampleHandler extends DefaultHandler{

	// ===========================================================
	// Fields
	// ===========================================================

	private boolean in_item = false;
	private boolean in_title = false;
	private boolean in_itunesauthor = false;
	private boolean in_itunessummary = false;
	private boolean in_pubDate = false;
	private boolean in_itunesduration = false;
	private boolean in_link = false;
	private boolean in_url = false;
	private boolean in_foto = false;
	private boolean author_found = false;
	private boolean in_enclosure = false;
	private boolean in_guid = false;


	private int j;
	private int limit = 9;

	//creo que esto lo estamos creando doble, aqui y en startdoc.
	private StringBuilder buffer = new StringBuilder();

	//private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
	//creo que arriba sobra lo de = new... porque ya está abajo en startdocument.
	private Elemento[] myItemData;

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public Elemento[] getParsedData() {
		return this.myItemData;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myItemData = new Elemento[10];
		for(int i = 0; i < 10; i++)myItemData[i]=new Elemento();
		
		this.j = 0;
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/** Gets be called on opening tags like:
	 * <tag>
	 * Can provide attribute(s), when xml was like:
	 * <tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals("item")) {
			this.in_item = true;
		}else if (localName.equals("title")  && this.j<=limit && this.in_item ) {
			this.in_title = true;
		}else if (localName.equals("author") && this.j<=limit && this.in_item && !this.author_found) {
			this.in_itunesauthor = true;
		}else if (localName.equals("summary") && this.j<=limit && this.in_item) {
			this.in_itunessummary = true;
		}else if (localName.equals("pubDate") && this.j<=limit && this.in_item) {
			this.in_pubDate = true;
		}else if (localName.equals("duration") && this.j<=limit && this.in_item) {
			this.in_itunesduration = true;
		}else if (localName.equals("link") && this.j<=limit && this.in_item) {
			this.in_link = true;	    	                
		}else if (localName.equals("enclosure") &&this.j<=limit && this.in_item){
			myItemData[j].setURL(atts.getValue("url"));
		}else if (localName.equals("guid") &&this.j<=limit && this.in_item){
			this.in_guid = true;
		}else if (localName.equals("poster") &&this.j<=limit && this.in_item){
			myItemData[j].setFoto(atts.getValue("url"));
		}

		//Ejemplo de como extraer un attributevalue
		/*else if (localName.equals("tagwithnumber")) {
	                        // Extract an Attribute
	                        String attrValue = atts.getValue("thenumber");
	                        int i = Integer.parseInt(attrValue);
	                        myParsedExampleDataSet.setExtractedInt(i);
	                }*/
	}

	/** Gets be called on closing tags like:
	 * </tag> */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("item")){
			this.in_item = false;
			this.j++;
			this.author_found = false;
		}else if (localName.equals("title") && this.j<=limit && this.in_item) {
			this.in_title = false;
			String titulo = buffer.toString();
			myItemData[j].setTitles(titulo);
			buffer.delete(0, buffer.length());
		}else if (localName.equals("author") && this.j<=limit && this.in_item &&  !this.author_found) {
			this.in_itunesauthor = false;
			this.author_found = true;
			myItemData[j].setAuthor(buffer.toString());
			buffer.delete(0, buffer.length());
		}else if (localName.equals("summary") && this.j<=limit && this.in_item) {
			this.in_itunessummary = false;
			myItemData[j].setSummary(buffer.toString());
			buffer.delete(0, buffer.length());
		}else if (localName.equals("pubDate") && this.j<=limit && this.in_item) {
			this.in_pubDate = false;
			myItemData[j].setPubdate(buffer.toString());
			buffer.delete(0, buffer.length());
		}else if (localName.equals("duration") && this.j<=limit && this.in_item) {
			this.in_itunesduration = false;
			myItemData[j].setDuration(buffer.toString());
			buffer.delete(0, buffer.length());
		}else if (localName.equals("link") && this.j<=limit && this.in_item) {
			this.in_link = false;
			myItemData[j].setLink(buffer.toString());
			buffer.delete(0, buffer.length());
		}else if (localName.equals("guid") && this.j<=limit && this.in_item) {
			this.in_guid = false;
			//El id del podcast para usarlo en los comentarios esta despues del link, el numero que le sigue a "p="
			myItemData[j].setId(buffer.toString().split("p=")[1]);
			buffer.delete(0, buffer.length());
		}

	}

	/** Gets be called on the following structure:
	 * <tag>characters</tag> */
	@Override
	public void characters(char[] ch, int start, int length) {
		if (this.in_item){
			if(this.in_title || this.in_itunesauthor || this.in_itunessummary || this.in_pubDate || this.in_itunesduration ||this.in_guid || this.in_link){

				if (buffer!=null) {
					for (int i=start; i<start+length; i++) {
						buffer.append(ch[i]);
					}

				}
				else{
					//aquí habría que poner que pasa si buffer es nulo.
				}
			} //end if in_title or... or...

		} //end if in_item

	} //end proceso
} //end clase



